﻿package controllers.portal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import utils.NumberUtil;
import rbac.models.Oplog;
import rbac.models.User;

import models.Reply;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class ReplyController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
	public View getReplyItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		String orderBy = "id";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		**/
		if (q != null){
			cond += " and (clienname like '%"+q+"%' or clientmsg like '%"+q+"%' or clientphone  like '%"+q+"%'  ) ";			
		}
		

		Object[] args = tmpArgs.toArray();
		
		long total = Reply.count(Reply.class, cond, args);
		List<Reply> items = Reply.findAll(Reply.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	
	
	public View destroyReply(int id) throws Exception {
		Reply item = Reply.find(Reply.class, id);
		User user = (User) rbac.getCurrentUser();
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除留言信息，内容为：" + new JsonView(item));									

		item.destroy();
		return new JsonView("success:true");
	}
	
	public View batpassReply(String ids) throws Exception {
		if ((isAdmin()) && (ids!=null) && (NumberUtil.isNumbers(ids)))
		{
			User user = (User) rbac.getCurrentUser();
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")批量通过留言信息，编号为：" + ids);									

			Reply.execute("update Reply set  passflag = 0 where id in ("+ids+")", null);
		}
		return new JsonView("success:true");
	}
	
	public View batunpassReply(String ids) throws Exception {
		if ((isAdmin()) && (ids!=null) && (NumberUtil.isNumbers(ids)))
		{
			User user = (User) rbac.getCurrentUser();
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")批量取消通过留言信息，编号为：" + ids);									

			Reply.execute("update Reply set  passflag = -1 where id in ("+ids+")", null);
		}
		return new JsonView("success:true");
	}
	
	public View passReply(int id) throws Exception {
		if (isAdmin())
		{
			Reply item = Reply.find(Reply.class, id);
		item.setPassflag(0);
		item.save();
		User user = (User) rbac.getCurrentUser();
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")通过留言信息，内容为：" + new JsonView(item));									

		}
		return new JsonView("success:true");
	}
	
	public View unpassReply(int id) throws Exception {
		if (isAdmin())
		{
			Reply item = Reply.find(Reply.class, id);
		item.setPassflag(-1);
		item.save();
		User user = (User) rbac.getCurrentUser();
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")取消通过留言信息，内容为：" + new JsonView(item));									

		}
		return new JsonView("success:true");
	}
	
}
